Completed
Push — master ( e65836...fa4d5c )
by Mathieu
12s queued 10s
created

GetCustomersQueryHandler   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 2

1 Function

Rating   Name   Duplication   Size   Complexity  
A execute 0 28 2
1
import {Inject} from '@nestjs/common';
2
import {QueryHandler} from '@nestjs/cqrs';
3
import {GetCustomersQuery} from './GetCustomersQuery';
4
import {CustomerView} from '../View/CustomerView';
5
import {ICustomerRepository} from 'src/Domain/Customer/Repository/ICustomerRepository';
6
import {AddressView} from '../View/AddressView';
7
import {Pagination} from 'src/Application/Common/Pagination';
8
9
@QueryHandler(GetCustomersQuery)
10
export class GetCustomersQueryHandler {
11
  constructor(
12
    @Inject('ICustomerRepository')
13
    private readonly customerRepository: ICustomerRepository
14
  ) {}
15
16
  public async execute(
17
    query: GetCustomersQuery
18
  ): Promise<Pagination<CustomerView>> {
19
    const customerViews: CustomerView[] = [];
20
    const [customers, total] = await this.customerRepository.findCustomers(
21
      query.page
22
    );
23
24
    for (const customer of customers) {
25
      const address = customer.getAddress();
26
27
      customerViews.push(
28
        new CustomerView(
29
          customer.getId(),
30
          customer.getName(),
31
          new AddressView(
32
            address.getId(),
33
            address.getStreet(),
34
            address.getCity(),
35
            address.getZipCode(),
36
            address.getCountry()
37
          )
38
        )
39
      );
40
    }
41
42
    return new Pagination<CustomerView>(customerViews, total);
43
  }
44
}
45